<?php

/**
 * page actions.
 *
 * @package    quotes
 * @subpackage page
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class pageActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeContact(sfWebRequest $request)
  {
    $this->form = new ContactForm();

    if ($request->isMethod(sfRequest::POST))
    {
      $this->form->bind($request->getParameter('contact'));
      if($this->form->isValid())
      {
        $this->form->save();
        $this->getUser()->setFlash("notice", "Your message has been sent. We will try to respond as fast as possible!");
      }
    }
  }


  public function executeAbout(sfWebRequest $request)
  {

  }

  public function executeHome(sfWebRequest $request)
  {

  }

  public function executeSearch(sfWebRequest $request)
  {
    $this->form = new SearchForm();
    if ($request->isMethod(sfRequest::POST))
    {
      $request_params = $request->getParameter($this->form->getName());
      $this->keywords = $request_params['keywords'];

      if (isset($request_params['page']))
        $page = $request_params['page'];
      else
        $page = 1;

      if (isset($request_params['type']))
        $this->type = $request_params['type'];
      else
        $this->type = SearchForm::ALL;

      $this->form = new SearchForm(array(), array('def' => $this->keywords,'type_def' => $this->type ));

      if ($this->keywords != '')
      {
        if ($this->type && $this->type != SearchForm::ALL)
        {
          if ($this->type == SearchForm::AUTHORS)
            $this->authors = AuthorQuery::create()
              ->filterByIsActive(true)
              ->filterByName('%'.$this->keywords.'%')
              ->paginate($page, Tools::GENERAL_LIMIT);
          else
            $this->quotes = QuoteQuery::create()
              ->filterByIsActive(true)
              ->filterByContent('%'.$this->keywords.'%', Criteria::LIKE)
              ->paginate($page, Tools::GENERAL_LIMIT);
        }
        else
        {
          $this->authors = AuthorQuery::create()
            ->filterByIsActive(true)
            ->filterByName('%'.$this->keywords.'%')
            ->paginate(1, Tools::GENERAL_LIMIT);
          $this->quotes = QuoteQuery::create()
            ->filterByIsActive(true)
            ->filterByContent('%'.$this->keywords.'%', Criteria::LIKE)
            ->paginate(1, Tools::GENERAL_LIMIT);
        }
      }
    }
  }

  public function executeAutocomplete(sfWebRequest $request)
  {
    $this->query = $request->getParameter('query');
    $this->authors = AuthorQuery::create()
      ->filterByIsActive(true)
      ->filterByName('%'.$this->query.'%')
      ->find();
  }

  public function executeRegister(sfWebRequest $request)
  {
    $this->form = new RegisterForm();

    if ($request->isMethod(sfRequest::POST))
    {
      $this->form->bind($request->getParameter($this->form->getName()));

      if ($this->form->isValid())
      {
         $user = $this->form->save();
         $this->getUser()->signIn($user);
         $this->getUser()->setFlash("notice", "You have succesfully created an account. You are logged in and able to bookmark quotes and authors");
      }
    }
  }

  public function executeContribute(sfWebRequest $request)
  {
    $this->form = new ContributeForm();
    if ($request->isMethod(sfRequest::POST))
    {
      $this->form->bind($request->getParameter($this->form->getName()));
      if ($this->form->isValid())
      {
        $this->form->save();
        $this->getUser()->setFlash("notice", "Thanks for contributing! We will look at your suggestion as soon as possible.");
        $this->redirect('@register');
      }
    }
  }
}
